@echo off
setlocal enabledelayedexpansion

REM 获取当前日期和时间
for /f "tokens=1-3 delims= " %%a in ('date /t') do set CURRENT_DATE=%%a
for /f "tokens=1-2 delims=: " %%a in ('time /t') do set CURRENT_TIME=%%a:%%b

set timestamp=%CURRENT_DATE% %CURRENT_TIME%

set version=v1.0.0
set projectName=yunfan
set baseOutputDir=%projectName%-%version%

set architectures=amd64 arm64

call :package_for_arch amd64
call :package_for_arch arm64

goto :eof

:package_for_arch
set arch=%1
set outputDir=%baseOutputDir%-linux-%arch%
set binDir=%outputDir%\bin
set configDir=%outputDir%\etc
set logsDir=%outputDir%\logs
set tmpDir=%outputDir%\tmp
set packageDir=%outputDir%\package
set dependencyDir=%outputDir%\dependency
set tarGzFileName=%outputDir%.tar.gz

REM 删除现有目录（如果存在）
if exist "%outputDir%" (
    echo [%timestamp%] Delete path %outputDir%
    rd /s /q "%outputDir%"
)

if exist "%tarGzFileName%" (
    echo [%timestamp%] Delete file %tarGzFileName%
    del /q "%tarGzFileName%"
)

REM 创建输出目录结构
echo [%timestamp%] Create dir %outputDir%
mkdir "%outputDir%"
mkdir "%binDir%"
mkdir "%configDir%"
mkdir "%logsDir%"
mkdir "%tmpDir%"
mkdir "%packageDir%"
mkdir "%dependencyDir%"

REM 设置Go环境变量
set CGO_ENABLED=0
set GOOS=linux
set GOARCH=%arch%

REM 编译项目
echo [%timestamp%] Compiling projects for %arch%...
go build -ldflags="-s -w" -tags no_k8s -o "%binDir%\api-server" ..\api-server\api_server_main.go
go build -ldflags="-s -w" -tags no_k8s -o "%binDir%\agent-server" ..\rpc-server\agent_server_main.go
go build -ldflags="-s -w" -tags no_k8s -o "%binDir%\alert-server" ..\alert-server\alert_server_main.go
go build -ldflags="-s -w" -tags no_k8s -o "%binDir%\log-server" ..\log-server\log_server_main.go

REM 复制配置文件
echo [%timestamp%] Copying configuration files...
copy ..\etc\* "%configDir%"

REM 复制 package 目录中的文件到 packageDir
echo [%timestamp%] Copying scripts...
copy .\package\jdk-* "%packageDir%"
copy .\package\open* "%packageDir%"
REM copy .\package\kafka* "%packageDir%"
REM copy .\package\apache-zookeeper* "%packageDir%"
copy .\package\redis* "%packageDir%"
REM copy .\package\grafana* "%packageDir%"
copy .\package\alertmanager* "%packageDir%"
REM copy .\package\prometheus* "%packageDir%"
REM copy .\package\MinIO* "%packageDir%"
copy .\package\node* "%packageDir%"
copy .\dependencies\centos\* "%dependencyDir%"
copy .\dependencies\ubuntu\* "%dependencyDir%"

echo [%timestamp%] The build for %arch% is complete.

echo [%timestamp%] Packaging project for %arch% into tar.gz...
REM 打包为 tar.gz 文件（需要安装 7zip 或其他工具）
tar -czf "%tarGzFileName%" "%outputDir%"

echo [%timestamp%] Project for %arch% has been packaged into %tarGzFileName%
goto :eof
